#!/bin/sh

set -ex

index="${1}"
shift 1
cores="${1}"
shift 1

set +e

version="${1:-1.15.3}"
shift 1
#region="${1:-europe-west4-a}"
region="${1:-us-central1-a}"
shift 1
#network="${1:-tpu-euw4a}"
#network="${1:-tpu-usc1a}"
region_id="${1:-usc1a}"
shift 1

set +x

cidr="10.$(math $index + 2).0.0/$(python3 -c "import math; import sys; print(int(32 + 2 - math.log2(int(sys.argv[1]))))" ${cores})"

cat <<EOF
gcloud compute tpus create tpu-v2-${cores}-${region_id}-${index} --zone ${region} --project gpt-2-15b-poetry --network tpu-${region_id} --range ${cidr} --version ${version} --accelerator-type v2-${cores} --preemptible
EOF
